package com.jwdf.uda.ui.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.bean.UploadImage;
import com.jwdf.uda.databinding.FragmentGuideAddBinding;
import com.jwdf.uda.event.GuideAddEvent;
import com.jwdf.uda.event.GuideEvent;
import com.jwdf.uda.runtime.PermissionRequest;
import com.jwdf.uda.runtime.RuntimePermission;
import com.jwdf.uda.ui.activity.presenter.GuideAddPresenter;
import com.jwdf.uda.ui.activity.view.IGuideAddView;
import com.jwdf.uda.ui.fragment.base.BaseFragment;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.view.dialog.AlertDialog;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;

/**
 * 类说明：新增搭配指导
 */
public class GuideAddActivity extends AbsTitleBarActivity {
    private final static String GUIDE_ADD_USER = "guideAdd";

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, GuideAddActivity.class);
        context.startActivity(intent);
    }

    public static void startActivity(Context context , PbBean.PbUserInfo userSimple){
        Intent intent = new Intent(context, GuideAddActivity.class);
        intent.putExtra(GUIDE_ADD_USER, userSimple);
        context.startActivity(intent);
    }


    @Override
    protected Bundle onPutArguments() {
        Bundle args = new Bundle();
        args.putSerializable(GUIDE_ADD_USER, getIntent().getSerializableExtra(GUIDE_ADD_USER));
        return args;
    }

    @Override
    protected String getTopTitle() {
        return getString(R.string.title_guide_add);
    }

    @Override
    protected BaseFragment onCreateContentFragment() {
        return new GuideAddFragment();
    }

    @Override
    protected void setupTitleView() {
        super.setupTitleView();
        setOperateTitle(getString(R.string.head_operate_send));
    }

    @Override
    public void onHeadActionClick(View view) {
        super.onHeadActionClick(view);
        ((GuideAddFragment) mFragment).onHeadActionClick();
    }

    public static class GuideAddFragment extends BaseFragment implements IGuideAddView {
        private FragmentGuideAddBinding mBinding;
        private GuideAddPresenter mPresenter;
        private String mDesignerId;
        private PbBean.PbUserInfo mUserInfo;
        private final static String GUIDE_ADD_USER = "guideAdd";

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_guide_add, container, false);
            initView();
            return mBinding.getRoot();
        }

        private void initView() {
            checkPermission();
            mUserInfo=(PbBean.PbUserInfo)getArguments().getSerializable(GUIDE_ADD_USER);
            if(mUserInfo!=null){
                loadingDesigner(mUserInfo);
            }
            mPresenter = new GuideAddPresenter(this);
            EventBusUtil.registerEvent(mBinding.selectorImages);
            EventBusUtil.registerEvent(this);
            mBinding.rlSpecified.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivityFromFragment(SearchDesignerActivity.class);
                }
            });
        }

        @PermissionRequest(100)
        private void checkPermission() {
            String perms[] = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
            if (!RuntimePermission.hasPermissions(getContext(), perms)) {
                RuntimePermission.requestPermissions(this, getString(R.string.app_name),
                        100, perms);
            }
        }

        public void onEventMainThread(GuideEvent event) {
            switch (event.getEvent()) {
                case GuideEvent.EVENT_GUIDE_CHOOSE:
                    loadingDesigner(event.getUserSimple());
                    break;
            }
        }

        public void loadingDesigner(PbBean.PbUserSimple userSimple) {
            if (userSimple != null) {
                mBinding.rlDesigner.setVisibility(View.VISIBLE);
                Resources resource = (Resources)getActivity().getBaseContext().getResources();
                ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.fdc);
                if (csl != null) {
                    mBinding.tvUserName.setTextColor(csl);
                }
                mBinding.tvUserName.setText("重选");
                mDesignerId = userSimple.getUserId();
                ImageLoader.getInstance().displayImage(userSimple.getAvatar().getUrl(), mBinding.ivHeader);
                mBinding.tvNickName.setText(userSimple.getNickname());
                mBinding.tvIntroduction.setVisibility(TextUtils.isEmpty(userSimple.getIntroduction()) ? View.GONE : View.VISIBLE);
                mBinding.tvIntroduction.setText(userSimple.getIntroduction());
                mBinding.tvRealName.setVisibility(View.GONE);
                mBinding.ivIsDesigner.setVisibility(userSimple.getIsDesigner() ? View.VISIBLE : View.GONE);
            }
        }

        public void loadingDesigner(PbBean.PbUserInfo userInfo) {
            if (userInfo != null) {
                mBinding.rlDesigner.setVisibility(View.VISIBLE);
                Resources resource = (Resources)getActivity().getBaseContext().getResources();
                ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.fdc);
                if (csl != null) {
                    mBinding.tvUserName.setTextColor(csl);
                }
                mBinding.tvUserName.setText("重选");
                mDesignerId = userInfo.getUserId();
                ImageLoader.getInstance().displayImage(userInfo.getAvatar().getUrl(), mBinding.ivHeader);
                mBinding.tvNickName.setText(userInfo.getNickname());
                mBinding.tvIntroduction.setVisibility(TextUtils.isEmpty(userInfo.getIntroduction()) ? View.GONE : View.VISIBLE);
                mBinding.tvIntroduction.setText(userInfo.getIntroduction());
                mBinding.tvRealName.setVisibility(View.GONE);
                mBinding.ivIsDesigner.setVisibility(userInfo.getIsDesigner() ? View.VISIBLE : View.GONE);
            }
        }

        @Override
        public void onDestroyView() {
            super.onDestroyView();
            EventBusUtil.unregisterEvent(mBinding.selectorImages);
            EventBusUtil.unregisterEvent(this);
        }

        @Override
        public BaseFragment getFragment() {
            return this;
        }

        @Override
        public void onHeadActionClick() {
            mPresenter.onHeadActionClick();
        }

        @Override
        public String getContent() {
            return StringUtils.filterNullAndTrim(mBinding.etContent.getText().toString());
        }

        @Override
        public List<UploadImage> getUploadImageList() {
            return mBinding.selectorImages.getUploadImageList();
        }

        @Override
        public void setImageToken(UploadImage uploadImage) {
            mBinding.selectorImages.setImageToken(uploadImage);
        }

        @Override
        public String getDesignerId() {
            return mDesignerId;
        }

        @Override
        public void showSuccessDialog(final int guideId) {
            final AlertDialog dialog = new AlertDialog(getActivity(), getString(R.string.dialog_toast_guide_succee), new AlertDialog.CallbackListener() {
                @Override
                public void clickYes() {
                    getActivity().finish();
//                    GuideDetailActivity.startActivity(getActivity(), guideId);
                    EventBusUtil.post(new GuideAddEvent());
                }
            });
            dialog.show();
        }
    }
}
